package com.market.service.admin.impl;

import com.market.bean.MarketUser;
import com.market.bean.MarketUserExample;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;
import com.market.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.service.admin.AdminUserService;
import com.market.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author stone
 * @date 2022/06/25 11:11
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    MarketUserMapper marketUserMapper;


    /**
     * @param info     分页: page\limit    排序: sort\order
     * @param username
     * @param mobile
     * @return
     */
    @Override
    public CommonData<MarketUser> list(BasePageInfo info, String username, String mobile) {
        MarketUserExample example = new MarketUserExample();
        if (info != null && info.getLimit() != 0) {
            PageHelper.startPage(info.getPage(), info.getLimit());
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        } else {
            PageHelper.startPage(0, 0, true, true, true);
        }
        MarketUserExample.Criteria criteria = example.createCriteria();
        //判空 增加if之后成为条件查询
        if (!StringUtil.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!StringUtil.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketUser> list = marketUserMapper.selectByExample(example);
        PageInfo<MarketUser> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketUser update(MarketUser user) {
        Date date = new Date();
        user.setUpdateTime(date);
        marketUserMapper.updateByPrimaryKeySelective(user);
        return user;
    }

    @Override
    public MarketUser detail(Integer id) {
        MarketUser user = marketUserMapper.selectByPrimaryKey(id);
        return user;
    }
}
